﻿using System.Windows;
using DataBaseLibrary.DBObjectsFB;
using DataBaseLibrary.DBInterfaces;

namespace DocMreoLiteVersion
{
    /// <summary>
    /// Interaction logic for SettingWindow.xaml
    /// </summary>
    public partial class SettingWindow : Window
    {
        private IDBProvider mProvider;
        private RegisterComputerInfo r_info;
        public SettingWindow()
        {
            InitializeComponent();

            TextBoxPathDB.Text = CSettings.PathDB;
            CheckedPathCaontract.IsChecked = CSettings.SaleContractSettings.IsPrintNumberContract;
        }

        public SettingWindow(IDBProvider m_provider)
            : this()
        {
            this.mProvider = m_provider;
            try
            {
                r_info = new RegisterComputerInfo();
                mProvider.Load(r_info, "0");
                this.DataContext = r_info;
            }
            catch 
            {
                mProvider = null;
                MessageBox.Show("Ошибка при загрузке информации из БД", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                
            }

        }

        private void SaveButton_Click_1(object sender, RoutedEventArgs e)
        {
            CSettings.PathDB = TextBoxPathDB.Text;
            CSettings.SaleContractSettings.IsPrintNumberContract = CheckedPathCaontract.IsChecked ?? true;
            CSettings.SaveSettings();

            try
            {
                if (mProvider == null)
                    this.DialogResult = false;

                mProvider.Update(r_info);
            }
            catch
            {
                MessageBox.Show("Ошибка при обновлении информации в БД!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                //this.DialogResult = false;
            }

            //this.DialogResult = true;
        }

        private void CancelButton_Click_1(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
